# -*- coding: utf-8 -*-
# @FileName :conftest.py
# @Time :2024-01-28 18:53
# author: 小燕子

import pytest
import jsonpath
from ..Package.database import Sql


@pytest.fixture(autouse=True)
def request_data():
    return {"request": {
        "headers": {
            "content-type": "application/json"
        },
        "params": {
            "s": "api/user/login",
            "application": "app",
            "application_client_type": "weixin"
        },
        "json": {
            "accounts": "Qik1",
            "pwd": "123456",
            "type": "username"
        },

        "method": "post",
        "url": "http://shop-xo.hctestedu.com:80/index.php",
    },

        "assert_data":
            {"$.code": 0, "$.msg": "登录成功", "$.data.username": "Qik1"}
    }


@pytest.fixture(autouse=True)
def assert_operate():
    def func(assert_data, response_data):
        for k, v in assert_data.items():
            data = jsonpath.jsonpath(response_data, k)[0]
            assert data == v, f'业务断言失败，实际拿到的数据：{data}，预期的数据是：{v}'
        print("业务断言成功")

    return func  # 引用


@pytest.fixture(autouse=True)
def assert_sql_data():
    def func(expect_value, assert_data):
        data = Sql().execute(assert_data["dql"]).fetchone()
        expect_data = jsonpath.jsonpath(data, assert_data["sql_jsonpath"])[0]
        expect_value = jsonpath.jsonpath(expect_value, assert_data["r_jsonpath"])[0]
        assert expect_data == expect_value, f"数据断言失败，实际拿到的数据是{data},预期的数据是{assert_data}"
        print("数据断言成功")

    return func
